//
//  SettingMainViewController.m
//  HTP
//
//  Created by 陈钱钿 on 15/11/3.
//  Copyright © 2015年 nit. All rights reserved.
//

#import "SettingMainViewController.h"
#import "AccountSafeViewController.h"
#import "AboutAppViewController.h"
#import "Cache_clear.h"
#import <QuartzCore/QuartzCore.h>
#import "PlaceholderTextView.h"
#define kBorder 20
#define cellHeight 40
@interface SettingMainViewController () <UITextViewDelegate>
@property (nonatomic, retain) UIView *alterView;
@property (nonatomic, retain) UIView *firstView;
@property (nonatomic, retain) UIView *secondView;
@property (nonatomic, retain) UIView *thirdView;
@property (nonatomic, retain) UIView *fourthView;
@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) UIButton *exitBtn;
@property (nonatomic, retain) UITapGestureRecognizer *accGesture;
@property (nonatomic, retain) UITapGestureRecognizer *FBGesture;
@property (nonatomic, retain) UITapGestureRecognizer *aboutGesture;
@property (nonatomic, retain) UIButton *accountMore;
@property (nonatomic, retain) UIButton *cache;
@property (nonatomic, retain) UIButton *FBMore;
@property (nonatomic, retain) UIButton *aboutMore;

@end

@implementation SettingMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addCustomTitleWithTitle:@"设置"];
    [self addCustomBackWithAction:@selector(doBack) normalImg:[UIImage imageNamed:@"back"] selectImg:[UIImage imageNamed:@"back"] withPadding:5];
    [self initUI];
//    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
//    back.frame = CGRectMake(0, 20, 20, 40);
//    [back setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
//    [back addTarget:self action:@selector(doBack) forControlEvents:UIControlEventTouchUpInside];
//    
//    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:back];
//    self.navigationItem.leftBarButtonItem = backItem;
}


//TODO:绘制UI
- (void)initUI {
    self.view.backgroundColor = [UIColor colorWithRed:233.0/255 green:233.0/255 blue:233.0/255 alpha:1];
    
    self.firstView = [[UIView alloc] initWithFrame:CGRectMake(0, kBorder, ScreenWidth, cellHeight)];
    self.firstView.backgroundColor = [UIColor whiteColor];
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(kBorder, 0, 120, cellHeight)];
    label1.text = @"帐号与安全";
    [self.firstView addSubview:label1];
    self.accountMore = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth-30, 9.5, 13, 25)];
    [self.accountMore setBackgroundImage:[UIImage imageNamed:@"more"] forState:UIControlStateNormal];
    self.accGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(accountForward)];
    [self.firstView addSubview:self.accountMore];
    [self.firstView addGestureRecognizer:self.accGesture];
    [self.view addSubview:self.firstView];
    
    
    self.secondView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.firstView.frame)+kBorder, ScreenWidth, cellHeight+1)];
    self.secondView.backgroundColor = [UIColor whiteColor];
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(kBorder, 0, 120, cellHeight)];
    label2.text = @"清理缓存";
    [self.secondView addSubview:label2];
    UILabel *cacheSize = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth-34-40, 0, 40, cellHeight)];
    cacheSize.text = @"30M";
    cacheSize.textColor = [UIColor grayColor];
    [self.secondView addSubview:cacheSize];
    self.cache = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth-34, 11, 17, 20)];
    [self.cache setBackgroundImage:[UIImage imageNamed:@"huancun"] forState:UIControlStateNormal];
    [self.cache addTarget:self action:@selector(computerCache) forControlEvents:UIControlEventTouchUpInside];
    [self.secondView addSubview:self.cache];
    UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(kBorder, CGRectGetMaxY(label2.frame), ScreenWidth-2*kBorder, 1)];
    line.image = [UIImage imageNamed:@"settingline"];
    [self.secondView addSubview:line];
    [self.view addSubview:self.secondView];
    
    
    self.thirdView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.secondView.frame), ScreenWidth, cellHeight+1)];
    self.thirdView.backgroundColor = [UIColor whiteColor];
    UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(kBorder, 0, 120, cellHeight)];
    label3.text = @"反馈";
    [self.thirdView addSubview:label3];
    self.FBMore = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth-30, 9.5, 13, 25)];
    [self.FBMore setBackgroundImage:[UIImage imageNamed:@"more"] forState:UIControlStateNormal];
    [self.thirdView addSubview:self.FBMore];
    UIImageView *line2 = [[UIImageView alloc] initWithFrame:CGRectMake(kBorder, CGRectGetMaxY(label3.frame), ScreenWidth-2*kBorder, 1)];
    line2.image = [UIImage imageNamed:@"settingline"];
    [self.thirdView addSubview:line2];
    self.FBGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(FBforwad)];
    [self.thirdView addGestureRecognizer:self.FBGesture];
    [self.view addSubview:self.thirdView];
    
    
    self.fourthView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.thirdView.frame), ScreenWidth, cellHeight)];
    self.fourthView.backgroundColor = [UIColor whiteColor];
    UILabel *label4 = [[UILabel alloc] initWithFrame:CGRectMake(kBorder, 0, 120, cellHeight)];
    label4.text = @"关于青苗";
    [self.fourthView addSubview:label4];
    self.aboutMore = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth-30, 9.5, 13, 25)];
    [self.aboutMore setBackgroundImage:[UIImage imageNamed:@"more"] forState:UIControlStateNormal];
    self.aboutGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(aboutForward)];
    [self.fourthView addGestureRecognizer:self.aboutGesture];
    [self.fourthView addSubview:self.aboutMore];
    [self.view addSubview:self.fourthView];
    
    self.exitBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.fourthView.frame)+kBorder, ScreenWidth, cellHeight)];
    [self.exitBtn setBackgroundColor:[UIColor whiteColor]];
    [self.exitBtn setTitle:@"退出当前帐号" forState:UIControlStateNormal];
    [self.exitBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [self.view addSubview:self.exitBtn];
}
/**
 *  yunhe 计算缓存大小
 */
-(void)computerCache{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *path = [paths lastObject];
    DLog(@"path --- >%@",path);
    Cache_clear *cache_clear = [[Cache_clear alloc] init];
    DLog(@"%lf",[cache_clear folderSizeAtPath:path]);
}
/*
 cqt 进入账户与安全
 */
- (void)accountForward {
    AccountSafeViewController *asVc = [[AccountSafeViewController alloc] init];
    [self.navigationController pushViewController:asVc animated:YES];
}

/*
 cqt 进入关于青苗
 */
- (void)aboutForward {
    AboutAppViewController *aaVc = [[AboutAppViewController alloc] init];
    [self.navigationController pushViewController:aaVc animated:YES];
}
/*
 cqt 反馈 弹框
 */
- (void)FBforwad {
    self.alterView = [[UIView alloc] initWithFrame:CGRectMake(kBorder, CGRectGetMaxY(_firstView.frame)+1+kBorder-5, ScreenWidth-2*kBorder, 2*kBorder+4*cellHeight)];
    self.alterView.layer.cornerRadius = 7;
    self.alterView.backgroundColor = [UIColor colorWithRed:233.0/255 green:233.0/255 blue:233.0/255 alpha:1];
    
    
    PlaceholderTextView *textView = [[PlaceholderTextView alloc] initWithFrame:CGRectMake(kBorder, kBorder, ScreenWidth-4*kBorder, kBorder+3*cellHeight)];
    textView.placeholder = @"输入反馈信息";
    textView.font = [UIFont systemFontOfSize:16.0];
    textView.delegate = self;
    [self.alterView addSubview:textView];
    
    UIButton *confirmBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(textView.frame), ScreenWidth-2*kBorder, cellHeight)];
    [confirmBtn setTitle:@"确认" forState:UIControlStateNormal];
    [confirmBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [confirmBtn addTarget:self action:@selector(confirmClick) forControlEvents:UIControlEventTouchUpInside];
    [self.alterView addSubview:confirmBtn];
    
    self.window = [[UIApplication sharedApplication] delegate].window;
    [self.view addSubview:self.alterView];
    self.view.backgroundColor = [UIColor colorWithRed:124.0/255 green:124.0/255 blue:124.0/255 alpha:1];
    self.firstView.backgroundColor = [UIColor colorWithRed:135.0/255 green:135.0/255 blue:135.0/255 alpha:1];
    self.secondView.backgroundColor = [UIColor colorWithRed:135.0/255 green:135.0/255 blue:135.0/255 alpha:1];
    self.thirdView.backgroundColor = [UIColor colorWithRed:135.0/255 green:135.0/255 blue:135.0/255 alpha:1];
    self.fourthView.backgroundColor = [UIColor colorWithRed:135.0/255 green:135.0/255 blue:135.0/255 alpha:1];
    self.exitBtn.backgroundColor = [UIColor colorWithRed:135.0/255 green:135.0/255 blue:135.0/255 alpha:1];
    
    self.FBGesture.enabled = NO;
    self.accGesture.enabled = NO;
    self.aboutGesture.enabled = NO;
    self.navigationItem.leftBarButtonItem.enabled = NO;
    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change) name:UITextViewTextDidBeginEditingNotification object:textView];
    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change) name:UITextFieldTextDidBeginEditingNotification object:self.addAddressField];
    
    //键盘隐藏的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHiden) name:UIKeyboardWillHideNotification object:nil];
}

///*
// TextView开始输入时 调用
// */
//- (void)change {
//
//}

#pragma mark 解决键盘挡住UITextView的方法
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    NSTimeInterval animationDuration=0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    int offset= -5;
    CGRect rect=CGRectMake(0.0f,offset,ScreenWidth,ScreenHeihgt);
    self.view.frame=rect;
    [UIView commitAnimations];
    return YES;
}

//恢复原始视图位置
-(void)resumeView
{
    NSTimeInterval animationDuration=0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    CGRect rect=CGRectMake(0.0f,64,ScreenWidth,ScreenHeihgt);
    self.view.frame=rect;
    [UIView commitAnimations];
}

//键盘隐藏时调用
-(void)keyboardWillHiden {
    [self resumeView];
}



/*
 cqt 点击弹框的确认按钮
 */
- (void)confirmClick {
    [self.alterView removeFromSuperview];
    
    self.view.backgroundColor = [UIColor colorWithRed:233.0/255 green:233.0/255 blue:233.0/255 alpha:1];
    
    self.firstView.backgroundColor = [UIColor whiteColor];
    self.secondView.backgroundColor = [UIColor whiteColor];
    self.thirdView.backgroundColor = [UIColor whiteColor];
    self.fourthView.backgroundColor = [UIColor whiteColor];
    self.exitBtn.backgroundColor = [UIColor whiteColor];
    
    
    self.FBGesture.enabled = YES;
    self.accGesture.enabled = YES;
    self.aboutGesture.enabled = YES;
    self.navigationItem.leftBarButtonItem.enabled = YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    //退出键盘
    [self.view endEditing:YES];
}

- (void)doBack{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
